package validate

import (
	"encoding/json"
	"github.com/stretchr/testify/assert"
	"github.com/wI2L/jsondiff"
	"testing"
)

func TestNilErrHandle(t *testing.T) {
	type args struct {
		i              int
		val            int
		last           int
		operationValue interface{}
	}
	tests := []struct {
		name string
		args args
		want interface{}
	}{
		{
			name: "test",
			args: args{
				i:              0,
				val:            0,
				last:           0,
				operationValue: nil,
			},
			want: []interface{}{0},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equalf(t, tt.want, NilErrHandle(tt.args.i, tt.args.val, tt.args.last, tt.args.operationValue), "NilErrHandle(%v, %v, %v, %v)", tt.args.i, tt.args.val, tt.args.last, tt.args.operationValue)
		})
	}
}

func TestConvertor(t *testing.T) {
	tests := []struct {
		name      string
		operation jsondiff.Operation
		want      string
	}{
		{
			name: "replace",
			operation: jsondiff.Operation{
				Value:    "test2",
				OldValue: "test3",
				Type:     "replace",
				From:     "",
				Path:     "/spec/containers/0/args/1",
			},
			want: "{\"spec\":{\"containers\":[{\"args\":{\"1\":\"test2\"}}]}}",
		},
		{
			name: "remove",
			operation: jsondiff.Operation{
				Value:    "test3",
				OldValue: "",
				Type:     "remove",
				From:     "",
				Path:     "/spec/containers/0/args/1",
			},
			want: "{\"spec\":{\"containers\":[{\"args\":{\"1\":\"test3\"}}]}}", //какая то фигня но чинить буду потом)
		},
		{
			name: "add",
			operation: jsondiff.Operation{
				Value:    "test2",
				OldValue: "",
				Type:     "add",
				From:     "",
				Path:     "/spec/containers/0/args/-",
			},
			want: "{\"spec\":{\"containers\":[{\"args\":[\"test2\"]}]}}",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			data := Convert2JSONMainFunc(tt.operation.Type, tt.operation, tt.operation.Value)
			out, _ := json.Marshal(data)
			assert.Equalf(t, tt.want, string(out), "Operation %s failed. Result %s", tt.name, string(out))
		})
	}
}

func TestReplacePatchReflectCasesHandle(t *testing.T) {
	type args struct {
		element interface{}
		patches []jsondiff.Operation
		path    string
	}
	tests := []struct {
		name  string
		args  args
		want  bool
		want1 []jsondiff.Operation
	}{
		{
			name: "empty element",
			args: args{
				element: []interface{}{},
				patches: []jsondiff.Operation(nil),
				path:    "",
			},
			want:  true,
			want1: []jsondiff.Operation(nil),
		},
		{
			name: "filled element",
			args: args{
				element: []interface{}{"test"},
				patches: []jsondiff.Operation(nil),
				path:    "",
			},
			want: false,
			want1: []jsondiff.Operation{{
				Value:    "test",
				OldValue: nil,
				Type:     "replace",
				From:     "",
				Path:     "/0",
			}},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, got1 := ReplacePatchReflectCasesHandle(tt.args.element, tt.args.patches, tt.args.path)
			assert.Equalf(t, tt.want, got, "ReplacePatchReflectCasesHandle(%v, %v, %v)", tt.args.element, tt.args.patches, tt.args.path)
			assert.Equalf(t, tt.want1, got1, "ReplacePatchReflectCasesHandle(%v, %v, %v)", tt.args.element, tt.args.patches, tt.args.path)
		})
	}
}
